package com.yupi.springbootinit.utils.file;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class FileComparator {
    public static void main(String[] args) {
        String file1Path = "path/to/your/file1.txt";
        String file2Path = "path/to/your/file2.txt";

        try {
            if (areFilesEqual(file1Path, file2Path)) {
                System.out.println("Files are equal");
            } else {
                System.out.println("Files are not equal");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static boolean areFilesEqual(String filePath1, String filePath2) throws IOException {
        try (BufferedReader reader1 = new BufferedReader(new FileReader(filePath1));
             BufferedReader reader2 = new BufferedReader(new FileReader(filePath2))) {

            String line1, line2;
            while ((line1 = reader1.readLine()) != null & (line2 = reader2.readLine()) != null) {
                if (!line1.equals(line2)) {
                    return false; // 如果发现行内容不相等，则文件不相等
                }
            }

            // 如果文件长度不同，则文件不相等
            return line1 == null && line2 == null;
        }
    }
}
